/// 基础响应模型 - Base response model
/// 用于统一处理所有API接口的返回值结构
class BaseResponse<T> {
  /// 请求是否成功 - Whether the request was successful
  final bool success;
  
  /// 响应消息 - Response message
  final String message;
  
  /// 响应数据 - Response data
  final T? data;
  
  /// 错误代码 - Error code
  final int? code;
  
  /// 时间戳 - Timestamp
  final int? timestamp;

  const BaseResponse({
    required this.success,
    required this.message,
    this.data,
    this.code,
    this.timestamp,
  });

  /// 从JSON创建BaseResponse实例 - Create BaseResponse instance from JSON
  factory BaseResponse.fromJson(
    Map<String, dynamic> json,
    T Function(dynamic)? fromJsonT,
  ) {
    return BaseResponse<T>(
      success: json['success'] ?? false,
      message: json['message'] ?? '',
      data: json['data'] != null && fromJsonT != null 
          ? fromJsonT(json['data']) 
          : json['data'],
      code: json['code'],
      timestamp: json['timestamp'],
    );
  }

  /// 转换为JSON - Convert to JSON
  Map<String, dynamic> toJson() {
    return {
      'success': success,
      'message': message,
      'data': data,
      'code': code,
      'timestamp': timestamp,
    };
  }

  /// 创建成功响应 - Create success response
  factory BaseResponse.success({
    required T data,
    String message = '操作成功',
    int? code,
  }) {
    return BaseResponse<T>(
      success: true,
      message: message,
      data: data,
      code: code,
      timestamp: DateTime.now().millisecondsSinceEpoch,
    );
  }

  /// 创建失败响应 - Create error response
  factory BaseResponse.error({
    required String message,
    int? code,
    T? data,
  }) {
    return BaseResponse<T>(
      success: false,
      message: message,
      data: data,
      code: code,
      timestamp: DateTime.now().millisecondsSinceEpoch,
    );
  }

  /// 判断是否为成功响应 - Check if response is successful
  bool get isSuccess => success;

  /// 判断是否为失败响应 - Check if response is error
  bool get isError => !success;
}

/// 分页数据模型 - Pagination data model
class PaginationData<T> {
  /// 当前页码 - Current page number
  final int currentPage;
  
  /// 每页数量 - Items per page
  final int pageSize;
  
  /// 总数量 - Total count
  final int total;
  
  /// 总页数 - Total pages
  final int totalPages;
  
  /// 数据列表 - Data list
  final List<T> items;

  const PaginationData({
    required this.currentPage,
    required this.pageSize,
    required this.total,
    required this.totalPages,
    required this.items,
  });

  /// 从JSON创建PaginationData实例 - Create PaginationData instance from JSON
  factory PaginationData.fromJson(
    Map<String, dynamic> json,
    T Function(Map<String, dynamic>) fromJsonT,
  ) {
    return PaginationData<T>(
      currentPage: json['currentPage'] ?? 1,
      pageSize: json['pageSize'] ?? 10,
      total: json['total'] ?? 0,
      totalPages: json['totalPages'] ?? 0,
      items: (json['items'] as List<dynamic>?)
          ?.map((item) => fromJsonT(item as Map<String, dynamic>))
          .toList() ?? [],
    );
  }

  /// 转换为JSON - Convert to JSON
  Map<String, dynamic> toJson() {
    return {
      'currentPage': currentPage,
      'pageSize': pageSize,
      'total': total,
      'totalPages': totalPages,
      'items': items,
    };
  }

  /// 是否有下一页 - Whether there is a next page
  bool get hasNextPage => currentPage < totalPages;

  /// 是否有上一页 - Whether there is a previous page
  bool get hasPreviousPage => currentPage > 1;

  /// 是否为空 - Whether the list is empty
  bool get isEmpty => items.isEmpty;

  /// 是否不为空 - Whether the list is not empty
  bool get isNotEmpty => items.isNotEmpty;
}